import ImageCompressor from '@/static/image-compressor.min.js';
import {
	getPicture
} from '@/api/doc.js' //获取图片
import line from '../uni_modules/uview-ui/libs/config/props/line';
let mixin = {
	data() {
		return {}
	},
	methods: {
		// 获取前几天后几天日期
		getDay(day) {
			var today = new Date();
			var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
			today.setTime(targetday_milliseconds);
			var tYear = today.getFullYear();
			var tMonth = today.getMonth();
			var tDate = today.getDate();
			let tHours = today.getHours();
			let tMinutes = today.getMinutes();
			let tSeconds = today.getSeconds();
			if (tHours < 10) {
				tHours = "0" + tHours
			}
			if (tMinutes < 10) {
				tMinutes = "0" + tMinutes
			}
			if (tSeconds < 10) {
				tSeconds = "0" + tSeconds
			}
			tMonth = this.doHandleMonth(tMonth + 1);
			tDate = this.doHandleMonth(tDate);
			return tYear + "-" + tMonth + "-" + tDate;
			//return tYear+"-"+tMonth+"-"+tDate+" "+ tHours+":"+tMinutes+":"+tSeconds;
		},
		doHandleMonth(month) {
			var m = month;
			if (month.toString().length == 1) {
				m = month;
			}
			return m;
		},
		// 转换日期 获得年月日 
		getDate(date, type = 1) {
			if(!date) return ''
			var d = new Date(date);
			let y = d.getFullYear();
			let m = d.getMonth() + 1;
			let did = d.getDate();
			let h = d.getHours();
			if (m < 10) {
				m = '0' + m
			};
			if (did < 10) {
				did = '0' + did
			};
			if (h < 10) {
				h = '0' + h
			};
			let minute = d.getMinutes();
			if (minute < 10) {
				minute = '0' + minute
			};
			let second = d.getSeconds();
			if (second < 10) {
				second = '0' + second
			};
			if(type === 1) {
				return y + '-' + m + '-' + did
			}else {
				return y + '/' + m + '/' + did
			}
			
		},
		// 转换日期 获得年月日
		getToDayTime() {
			var d = new Date();
			let y = d.getFullYear();
			let m = d.getMonth() + 1;
			let did = d.getDate();
			if (m < 10) {
				m = '0' + m
			};
			if (did < 10) {
				did = '0' + did
			};
			let minute = d.getMinutes();
			if (minute < 10) {
				minute = '0' + minute
			};
			let second = d.getSeconds();
			if (second < 10) {
				second = '0' + second
			};
			let now = (y + '-' + m + '-' + did)
			let date = new Date(Date.parse(now.replace(/-/g, "/")));
			return date.getTime()
		},
		// 转换日期 获得年月日 时分秒
		getDate2(date) {
			if(date) {
				var d = new Date(date);
				let y = d.getFullYear();
				let m = d.getMonth() + 1;
				let did = d.getDate();
				let h = d.getHours();
				if (m < 10) {
					m = '0' + m
				}
				if (did < 10) {
					did = '0' + did
				}
				if (h < 10) {
					h = '0' + h
				};
				let minute = d.getMinutes();
				if (minute < 10) {
					minute = '0' + minute
				};
				let second = d.getSeconds();
				if (second < 10) {
					second = '0' + second
				};
				return y + '-' + m + '-' + did + ' ' + h + ":" + minute + ':' + second
			}else{
				return ''
			}
			
		},
		getDate3(date) {
			if (date) {
				var d = new Date(date);
				let y = d.getFullYear();
				let m = d.getMonth() + 1;
				let did = d.getDate();
				let h = d.getHours();
				if (m < 10) {
					m = '0' + m
				}
				if (did < 10) {
					did = '0' + did
				}
				if (h < 10) {
					h = '0' + h
				};
				let minute = d.getMinutes();
				if (minute < 10) {
					minute = '0' + minute
				};
				let second = d.getSeconds();
				if (second < 10) {
					second = '0' + second
				};
				return y + '年' + m + '月' + did + '日' +  ' ' + h + ":" + minute + ':' + second
			} else {
				return ''
			}
		
		},

		// 获取星期几  this.getWeek(new Date(2020-10-20))
		getWeek(date) {
			var week;
			if (date.getDay() == 0) week = "星期日"
			if (date.getDay() == 1) week = "星期一"
			if (date.getDay() == 2) week = "星期二"
			if (date.getDay() == 3) week = "星期三"
			if (date.getDay() == 4) week = "星期四"
			if (date.getDay() == 5) week = "星期五"
			if (date.getDay() == 6) week = "星期六"
			return week;
		},

		/**
		 *获取几个月前的输入日期
		 *{param:DateTime} date 输入日期(YYYY-MM-DD)
		 *{param:number } monthNum 月数
		 */
		GetPreMonthDay(date, monthNum) {
			var dateArr = date.split('-');
			var year = dateArr[0]; //获取当前日期的年份
			var month = dateArr[1]; //获取当前日期的月份
			var day = dateArr[2]; //获取当前日期的日
			var days = new Date(year, month, 0);
			days = days.getDate(); //获取当前日期中月的天数
			var year2 = year;
			var month2 = parseInt(month) - monthNum;
			if (month2 <= 0) {
				var absM = Math.abs(month2);
				year2 = parseInt(year2) - Math.ceil(absM / 12 == 0 ? 1 : parseInt(absM) / 12);
				month2 = 12 - (absM % 12);
			}
			var day2 = day;
			var days2 = new Date(year2, month2, 0);
			days2 = days2.getDate();
			if (day2 > days2) {
				day2 = days2;
			}
			if (month2 < 10) {
				month2 = '0' + month2;
			}
			var t2 = year2 + '-' + month2 + '-' + day2;
			return t2;
		},

		/**
		 *获取下一个月的输入日期
		 *{param:DateTime} date 输入日期(YYYY-MM-DD)
		 *{param:number } monthNum 月数
		 */
		GetNextMonthDay(date, monthNum) {
			var dateArr = date.split('-');
			var year = dateArr[0]; //获取当前日期的年份
			var month = dateArr[1]; //获取当前日期的月份
			var day = dateArr[2]; //获取当前日期的日
			var days = new Date(year, month, 0);
			days = days.getDate(); //获取当前日期中的月的天数
			var year2 = year;
			var month2 = parseInt(month) + parseInt(monthNum);
			if (month2 > 12) {
				year2 = parseInt(year2) + parseInt((parseInt(month2) / 12 == 0 ? 1 : parseInt(month2) / 12));
				month2 = parseInt(month2) % 12;
			}
			var day2 = day;
			var days2 = new Date(year2, month2, 0);
			days2 = days2.getDate();
			if (day2 > days2) {
				day2 = days2;
			}
			if (month2 < 10) {
				month2 = '0' + month2;
			}

			var t2 = year2 + '-' + month2 + '-' + day2;
			return t2;
		},
		/**
		 * 获取AddDayCount天后的日期
		 * @param AddDayCount  XX后  正整数  XX前 -正整数
		 * @returns {string}
		 * @constructor
		 */

		GetDateStr(AddDayCount) {
			const dd = new Date();
			dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
			const y = dd.getFullYear();
			const m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期，不足10补0
			const d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0
			return y + "-" + m + "-" + d;
		},


		//通过身份证号计算年龄、性别、出生日期
		// userCard  身份证号
		// num == 1 获取出生日期
		// num == 2 获取性别
		// num == 3 获取年龄
		idCard(userCard, num) {
			//获取出生日期
			if (num == 1) {
				birth = userCard.substring(6, 10) + "-" + userCard.substring(10, 12) + "-" + userCard.substring(12,
					14);
				return birth;
			}
			//获取性别
			if (num == 2) {
				if (parseInt(userCard.substr(16, 1)) % 2 == 1) {
					return "男";
				} else {
					return "女";
				}
			}
			//获取年龄
			if (num == 3) {
				var myDate = new Date();
				var month = myDate.getMonth() + 1;
				var day = myDate.getDate();
				var age = myDate.getFullYear() - userCard.substring(6, 10) - 1;
				if (userCard.substring(10, 12) < month || userCard.substring(10, 12) == month && userCard.substring(
						12, 14) <= day) {
					age++;
				}
				return age;
			}
		},

		/**
		 * @description:  数组对象相同值相加 去重  (keyName相同, keyValue就相加)
		 * @param {type}  arr: 需要处理的数组  (原数组)
		 * @param {type}  keyName: 用于判断相同的键名 
		 * @param {type}  keyValue: 用于计算键值
		 * @return:       返回值: 数组
		 */
		// DelSameObjValue(list,'id','num')
		DelSameObjValue(arr, keyName, keyValue) {
			let baseArr = []
			let newArr = []
			for (let key in arr) {
				if (baseArr.includes(arr[key][keyName])) {
					newArr[baseArr.indexOf(arr[key][keyName])][keyValue] += arr[key][keyValue]
				} else {
					baseArr.push(arr[key][keyName])
					newArr.push(arr[key])
				}
			};
			return newArr
		},

		// 数组转树形数组
		// data 原数组
		// parent 父id
		filterArray(data, parent) {
			var tree = []
			var temp
			for (var i = 0; i < data.length; i++) {
				if (data[i].parentId == parent) {
					var obj = data[i]
					temp = filterArray(data, data[i].id)
					if (temp.length > 0) {
						obj.children = temp
					}
					tree.push(obj)
				}
			}
			return tree
		},
		getContent(obj) {
			if(obj.lineSiteSectionType === 'qj') {
				const {
						mileage = '',
						startMileageFront = '',
						startMileageAfter = '',
						endMileageFront = '',
						endMileageAfter = '',
						startNumber = '',
						endNumber = '',
						others = ''
				} = obj;
				let str =
					`${mileage}${startMileageFront}+${startMileageAfter}~${mileage}${endMileageFront}+${endMileageAfter}`
				if (!startMileageFront || !startMileageAfter || !endMileageFront || !endMileageAfter) str = ''
				let str2 = `${startNumber}环-${endNumber}环`
				if (!startNumber || !endNumber) str2 = ''
				let str3 = others
				if (!others) str3 = ''
				return `${str}${str&&str2 ? ',' : ''}${str2}${(str && str3) || (str2 && str3) ? ',' : ''}${str3}`;
			}else if(obj.lineSiteSectionType === 'cz') {
				const {
					stationLocation = '',
					others = ''
				} = obj;
				let str1 = stationLocation
				 let str2 = others
				if(!stationLocation) str1 = ''
				if(!others) str2 = ''
				return `${str1}${str1&&str2 ? ',' : ''}${str2}`
			}
			
		},
		getContent2(obj) {
				const {
						mileage = '',
						startMileageFront = '',
						startMileageAfter = '',
						endMileageFront = '',
						endMileageAfter = '',
						startNumber = '',
						endNumber = ''
				} = obj;
				let str =
					`${mileage}${startMileageFront}+${startMileageAfter}~${mileage}${endMileageFront}+${endMileageAfter}`
				if (!startMileageFront || !startMileageAfter || !endMileageFront || !endMileageAfter) str = ''
				return `${str}`;
		},
		getRingNum(obj) {
			const {
				startNumber = '', endNumber = ''
			} = obj;
			let str1 = `${startNumber}${startNumber && endNumber ? '~' : ''}${endNumber}`
			if (!startNumber || !endNumber) str1 = ''
			return `${str1}`
		},
		//针对杭港业主 替换位置
		getContentByHg(obj) {
			if(obj.lineSiteSectionType === 'qj') {
				const {
						mileage = '',
						startMileageFront = '',
						startMileageAfter = '',
						endMileageFront = '',
						endMileageAfter = '',
						startNumber = '',
						endNumber = '',
						others = ''
				} = obj;
				let str = `${startNumber}环-${endNumber}环`
				if (!startNumber || !endNumber) str = ''
				let str2 =
					`${mileage}${startMileageFront}+${startMileageAfter}~${mileage}${endMileageFront}+${endMileageAfter}`
				if (!startMileageFront || !startMileageAfter || !endMileageFront || !endMileageAfter) str2 = ''
				let str3 = others
				if (!others) str3 = ''
				return `${str}${str&&str2 ? ',' : ''}${str2}${(str && str3) || (str2 && str3) ? ',' : ''}${str3}`;
			}else if(obj.lineSiteSectionType === 'cz') {
				const {
					stationLocation = '',
					others = ''
				} = obj;
				let str1 = stationLocation
				 let str2 = others
				if(!stationLocation) str1 = ''
				if(!others) str2 = ''
				return `${str1}${str1&&str2 ? ',' : ''}${str2}`
			}
			
		},
		
		//模糊搜索
		mapTree(value, arr) {
			let newarr = [];
			arr.forEach(element => {
				if (element.name.indexOf(value) > -1) { // 判断条件
					newarr.push(element);
				} else {
					if (element.list && element.list.length > 0) {
						let redata = this.mapTree(value, element.list);
						if (redata && redata.length > 0) {
							let obj = {
								...element,
								list: redata
							};
							newarr.push(obj);
						}
					}
				}
			});
			return newarr;
		},
		//过滤树形数据
		filterData(lines) {
			let num = 0
			for(let it1 of lines) {
				for(let it2 of it1.list) {
					for(let it3 of it2.list) {
						it3.list = it3.list.filter(item =>{
							num += (item.amount || 0)
							return !!item.amount
						})
					}
					it2.list = it2.list.filter(it=>it.list.length)
				}
				it1.list = it1.list.filter(it=>it.list.length)
			}
			lines = lines.filter(it=>it.list.length)
			lines.num = num
			return lines			
		},
		/** 找出两个对象数组不同的元素 */
		getArrDifSameValue(arr1,arr2){
		    var result = [];
		    for(var i = 0; i < arr2.length; i++){
		        var obj = arr2[i];
		        var id = obj.id;
		        var isExist = false;
		        for(var j = 0; j < arr1.length; j++){
		            var aj = arr1[j];
		            var n = aj.id;
		            if(n == id){
		                isExist = true;
		                break;
		            }
		        }
		        if(!isExist){
		            result.push(obj);
		        }
		    }
		    return result;
		},
		getDifference(arr1, arr2) {
			return arr1.concat(arr2).filter((v, i, arr) => {
				return arr.indexOf(v) === arr.lastIndexOf(v)
			})
		},
		
	},
}
export default mixin
